package com.example.hfut_edu;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.HandlerThread;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class page_curriculum extends AppCompatActivity {
    public static page_curriculum curriculum_obj = null;
    private HandlerThread handlerThread;
    WebView webView;

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_curriculum);
        curriculum_obj = this;


        webView = (WebView) findViewById(R.id.local_webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.getSettings().setDomStorageEnabled(true);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webView.getSettings().setAppCachePath(appCachePath);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);

        //将一个java类对象传递到HTML页面中，
        webView.addJavascriptInterface(this, "JSBridge");
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");
        //loginpage.login_obj.finish();

        if(rwSPdata("r","islogin","").contains("True")){
            try {
                //MainActivity.main_obj.finish();
                //homepage.home_obg.finish();
                page_login.login_obj.finish();
            }catch (Exception e){
                //Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
            }

        }

    }




    public String rwSPdata(String flag,String key,String value){
        //email,passwd,vrcode
        SharedPreferences testSP= getSharedPreferences("testSP", Activity.MODE_PRIVATE);
        switch (flag){
            case "w":
                SharedPreferences.Editor editor = testSP.edit();
                editor.putString( key, value );
                editor.apply();
                return "";
            case "r":
                return testSP.getString( key, "" );
            default:
                return "";
        }
    }

    @JavascriptInterface
    public void showSometh(final String contentStr) {//js->原生，改变原生标题
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(contentStr.contains("-")){
                    Toast.makeText(page_curriculum.this, "已经是第一个教学周了", Toast.LENGTH_SHORT).show();
                }else if(contentStr.contains("+")){
                    Toast.makeText(page_curriculum.this, "已经是最后一个教学周了", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @JavascriptInterface
    public void getUserdata() {//js->原生->js
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //读取本地SD里储存的账号密码，向服务器请求信息
                if(rwSPdata("r","islogin","").contains("True")){
                    String account = rwSPdata("r","jw_account","");
                    String passwd = rwSPdata("r","jw_passwd", "");
                    String data_url ="getcourse?name="+account+"&passwd="+passwd;
                    webView.loadUrl("javascript:java_savedata('"+data_url+"')");
                }else {
                    denglupage();
                }
            }
        });
    }


    //Toast.makeText(MainActivity.this, "登陆" , Toast.LENGTH_SHORT).show();
    //Intent intent=new Intent(MainActivity.this, MainActivity2.class);
    //startActivity(intent);

    @JavascriptInterface
    public void denglupage() {//js->原生，
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(page_curriculum.this, "登陆" , Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(page_curriculum.this, page_login.class);
                intent.putExtra("extra_data","教务系统登陆");
                startActivity(intent);
            }
        });
    }

    @JavascriptInterface
    public void viewHome() {//js->原生，
        //Log.d("TAG", "contentStr:" + contentStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(page_curriculum.this, "home" , Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(page_curriculum.this, page_home.class);
                startActivity(intent);
            }
        });
    }







}